package br.edu.ufcg.dsc.lsi.vademecum;

import java.sql.SQLException;
import java.util.Iterator;

import com.hp.hpl.jena.assembler.assemblers.ReasonerFactoryAssembler;
import com.hp.hpl.jena.db.DBConnection;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ModelMaker;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.reasoner.Reasoner;
import com.hp.hpl.jena.reasoner.ReasonerFactory;
import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner;
import com.hp.hpl.jena.reasoner.rulesys.OWLFBRuleReasoner;
import com.hp.hpl.jena.reasoner.rulesys.OWLFBRuleReasonerFactory;
import com.hp.hpl.jena.reasoner.rulesys.impl.oldCode.OWLRuleReasoner;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;

public class VadeMecum {
	
//	private final static String ONTOLOGY = "http://lsi.dsc.ufcg.edu.br/vademecum.owl";
//	private final static String DB_URL = "jdbc:postgresql://150.165.75.168:5432/vademecum";
	private final static String ONTOLOGY_LOCATION = "C:\\Hugo\\ontologia\\vademecum.owl";
	private final static String ONTOLOGY_URI = "http://lsi.dsc.ufcg.edu.br/vademecum.owl";
	private final static String DB_URL = "jdbc:postgresql://localhost:5432/vademecum";
	
	private final static String DB_USER = "vademecum";
	private final static String DB_PASSWORD = "vademecum";
	private final static String DB_TYPE = "PostgreSQL";
	
	public VadeMecum() {
		// TODO Auto-generated constructor stub
	}
	
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		//createRDBModel();
		DBConnection connection = createConnection();
		ModelMaker maker = ModelFactory.createModelRDBMaker(connection);
		Model base = maker.openModel(ONTOLOGY_LOCATION);
        OntModel m = ModelFactory.createOntologyModel( createSpec(maker), base );
        ExtendedIterator listIndividuals = m.listStatements();
        while (listIndividuals.hasNext()) {
        	Statement c = (Statement) listIndividuals.next();
            System.out.println( "Class " + c.toString());
        }

	}

	private static void createRDBModel() throws ClassNotFoundException,
			SQLException {
		DBConnection connection = createConnection();
		// Get a ModelMaker for database-backed models
		ModelMaker maker = ModelFactory.createModelRDBMaker(connection);
		
		Model base = maker.createModel(ONTOLOGY_URI, true);
		
		OntModelSpec spec = createSpec(maker);
		
		OntModel m = ModelFactory.createOntologyModel( spec, base );

		m.read(ONTOLOGY_URI);
		connection.close();
	}

	private static OntModelSpec createSpec(ModelMaker maker) {
		OntModelSpec spec = new OntModelSpec( OntModelSpec.OWL_MEM );
        spec.setImportModelMaker( maker );
        
        ReasonerFactory factory = new OWLFBRuleReasonerFactory();
        OWLFBRuleReasoner reasoner = new OWLFBRuleReasoner(factory);
//		reasoner.
        
//        infmodel = ModelFactory.createInfModel(reasoner, m);
//		infmodel.prepare();
		
		return spec;
	}

	private static DBConnection createConnection()
			throws ClassNotFoundException {
		Class.forName("org.postgresql.Driver");
		
		// Create a database connection object
		DBConnection connection = new DBConnection(DB_URL, DB_USER, DB_PASSWORD, DB_TYPE);
//		connection.getDriver().setDoCompressURI(true);
		return connection;
	}
	
	
	
}
